<?php


namespace app\admin\controller\jskmsg;


use app\admin\model\jskmsg\Companyname;
use app\api\model\HomeOrderModel;
use app\api\model\UserModel;
use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;

/**
 * 家政订单数据统计管理
 *
 * @icon fa fa-circle-o
 */
class Homeordercount extends Backend
{
    protected $model = null;

    public function _initialize(){
        parent::_initialize();
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        //[$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $filter = $this->request->get("filter", '');
        $op = $this->request->get("op", '', 'trim');
        $sort = $this->request->get("sort", 'id');
        $order = $this->request->get("order", "DESC");
        $offset = $this->request->get("offset/d", 0);
        $limit = $this->request->get("limit/d", 10);
        $where = [];
        $apm = [];
        $filter = json_decode($filter,true);
        /**
        array(2) {
        ["name"]=>
        string(6) "李四"
        ["createtime"]=>
        string(41) "2022-10-16 00:00:00 - 2022-10-16 23:59:59"
        }
         */
        if(isset($filter['name'])){
            $where['name'] = ['like',"%{$filter['name']}%"];//家政公司名称
        }
        if(isset($filter['createtime'])){
            $str = explode('-',$filter['createtime']);//创建时间
            $start_time = $str[0];
            $end_time = $str[1];
            $apm['createtime'] = ['between time',[$start_time,$end_time]];
        }
        $list = Companyname::where('type',2)->where($where)->order($sort, $order)->paginate($limit);//所有家政公司名称
        if($list){
//            foreach ($list as &$company){
//                $company['home_count'] = UserModel::where(['company_name_id'=>$company['id'],'role'=>UserModel::ROLE_HOUSEKEEPER])->count();//家政员总数
//                $homes = UserModel::where(['company_name_id'=>$company['id'],'role'=>UserModel::ROLE_HOUSEKEEPER])->select();//相关的用户
//                $order_count = 0;//总订单
//                $order_money = 0;//总金额
//                foreach ($homes as $home) {
//                    //家政订单
//                    $order_count += HomeOrderModel::where('user_id',$home['id'])->where($apm)->count();//订单
//                    $order_money += HomeOrderModel::where('user_id',$home['id'])->where($apm)->sum('actual_price');//金额
//                }
//                $company['order_count'] = $order_count;
//                $company['order_money'] = number_format($order_money,2);
//            }
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }
}